package com.pktechan.manage.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.http.entity.ContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.Map.Entry;

/**
 * @program: gsPrivateSale19
 * @description:
 * @author: qx.zhang
 * @create: 2020-02-13 16:22
 **/
public class HttpIOUtil {

    private static Logger log = LoggerFactory.getLogger(HttpIOUtil.class);
    private final static String BOUNDARY = UUID.randomUUID().toString()
            .toLowerCase().replaceAll("-", "");// 边界标识
    private final static String PREFIX = "--";// 必须存在
    private final static String LINE_END = "\r\n";
    //超时时间
    private static final int MAX_TIMEOUT = 200000;

    /**
     * POST Multipart Request
     *
     * @param requestUrl 请求url
     * @param fileByte   请求上传的文件的Byte[]
     * @return FileResponseData
     * @throws Exception
     * @Description:
     */
    public static FileResponseData postSendFile(String requestUrl, byte[] fileByte) throws Exception {

        InputStream inputStream = new ByteArrayInputStream(fileByte);
        MultipartFile file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
        Map<String, String> requestText = new HashMap<>();
        Map<String, MultipartFile> requestFile = new HashMap<>();
        requestFile.put("file", file);
        return postSendFile(requestUrl, requestText, requestFile);
    }

    /**
     * POST Multipart Request
     *
     * @param requestUrl 请求url
     * @param fileByte   请求上传的文件 的二进制
     * @param fileName   请求上传的文件的名称 需要带文件后缀
     * @return FileResponseData
     * @throws Exception
     * @Description:
     */
    public static FileResponseData postSendFile(String requestUrl, byte[] fileByte, String fileName) throws Exception {
        InputStream inputStream = new ByteArrayInputStream(fileByte);
        MultipartFile file = new MockMultipartFile(fileName, fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
        Map<String, String> requestText = new HashMap<>();
        Map<String, MultipartFile> requestFile = new HashMap<>();
        requestFile.put("file", file);
        return postSendFile(requestUrl, requestText, requestFile);
    }

    /**
     * POST Multipart Request
     *
     * @param requestUrl 请求url
     * @param base64     请求上传的文件 的base64编码
     * @param fileName   请求上传的文件的名称 需要带文件后缀
     * @return FileResponseData
     * @throws Exception
     * @Description:
     */
    public static FileResponseData postSendFile(String requestUrl, String base64, String fileName) throws Exception {
        byte[] fileByte = new BASE64Decoder().decodeBuffer(base64);
        return postSendFile(requestUrl, fileByte, fileName);
    }


    /**
     * POST Multipart Request
     *
     * @param requestUrl 请求url
     * @param file       请求上传的文件
     * @return FileResponseData
     * @throws Exception
     * @Description:
     */
    public static FileResponseData postSendFile(String requestUrl, MultipartFile file) throws Exception {
        Map<String, String> requestText = new HashMap<>();
        Map<String, MultipartFile> requestFile = new HashMap<>();
        requestFile.put("file", file);
        return postSendFile(requestUrl, requestText, requestFile);
    }

    /**
     * POST Multipart Request
     *
     * @param requestUrl 请求url
     * @param file       请求上传的文件
     * @param fileName   请求上传的文件的名称 需要带文件后缀
     * @return FileResponseData
     * @throws Exception
     * @Description:
     */
    public static FileResponseData postSendFile(String requestUrl, MultipartFile file, String fileName) throws Exception {
        InputStream inputStream = new ByteArrayInputStream(file.getBytes());
        MultipartFile newFile = new MockMultipartFile(fileName, fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
        Map<String, String> requestText = new HashMap<>();
        Map<String, MultipartFile> requestFile = new HashMap<>();
        requestFile.put("file", file);
        return postSendFile(requestUrl, requestText, requestFile);
    }


    /**
     * POST Multipart Request
     *
     * @param requestUrl  请求url
     * @param requestText 请求参数
     * @param requestFile 请求上传的文件
     * @return
     * @throws Exception
     * @Description:
     */
    public static FileResponseData postSendFile(String requestUrl, Map<String, String> requestText, Map<String, MultipartFile> requestFile) throws Exception {
        String responseData = sendRequest(requestUrl, requestText, requestFile);
        FileResponseData fileResponseData = JSONObject.parseObject(responseData, FileResponseData.class);
        return fileResponseData;
    }

    /**
     * 获取fastdfs文件Base64
     *
     * @param requestUrl 请求url
     * @param filePath   请求获取的文件路径
     * @return
     * @throws Exception
     * @Description:
     */
    public static String getFileBase64(String requestUrl, String filePath) throws Exception {
        String base64 = postSendFilePath(requestUrl, filePath);
        return base64;
    }

    /**
     * 发送filePath参数
     *
     * @param requestUrl
     * @param filePath
     * @return
     * @throws Exception
     */
    private static String postSendFilePath(String requestUrl, String filePath) throws Exception {
        Map<String, String> requestText = new HashMap<>();
        requestText.put("filePath", filePath);
        Map<String, MultipartFile> requestFile = new HashMap<>();
        return sendRequest(requestUrl, requestText, requestFile);
    }

    /**
     * 获取文件访问TOKEN
     *
     * @param requestUrl 请求url
     * @param filePath   请求获取的文件路径
     * @return
     * @throws Exception
     * @Description:
     */
    public static FileResponseData getFileToken(String requestUrl, String filePath) throws Exception {
        String responseData = postSendFilePath(requestUrl, filePath);
        FileResponseData fileResponseData = JSONObject.parseObject(responseData, FileResponseData.class);
        return fileResponseData;
    }

    /**
     * 获取文件访问TOKEN
     *
     * @param requestUrl 请求url
     * @param filePath   请求获取的文件路径
     * @return
     * @throws Exception
     * @Description:
     */
    public static void downloadFile(String requestUrl, String filePath, HttpServletResponse response) throws Exception {
        Map<String, String> requestText = new HashMap<>();
        requestText.put("filePath", filePath);
        Map<String, MultipartFile> requestFile = new HashMap<>();
        download(requestUrl, requestText, requestFile, response);
    }

    /**
     * POST Multipart Request
     *
     * @param requestUrl  请求url
     * @param requestText 请求参数
     * @param requestFile 请求上传的文件
     * @return
     * @throws Exception
     * @Description:
     */
    public static void download(String requestUrl, Map<String, String> requestText, Map<String, MultipartFile> requestFile, HttpServletResponse response) throws Exception {
        HttpURLConnection conn = null;
        InputStream input = null;
        OutputStream os = null;
        BufferedReader br = null;
        try {
            URL url = new URL(requestUrl);
            conn = (HttpURLConnection) url.openConnection();

            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setConnectTimeout(MAX_TIMEOUT);
            conn.setReadTimeout(MAX_TIMEOUT);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Accept", "*/*");
            conn.setRequestProperty("Connection", "keep-alive");
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            conn.setRequestProperty("Charset", "UTF-8");
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
            conn.connect();

            // 往服务器端写内容 也就是发起http请求需要带的参数
            os = new DataOutputStream(conn.getOutputStream());
            // 请求参数部分
            writeParams(requestText, os);
            // 请求上传文件部分
            writeFile(requestFile, os);
            // 请求结束标志
            String endTarget = PREFIX + BOUNDARY + PREFIX + LINE_END;
            os.write(endTarget.getBytes());
            os.flush();

            OutputStream out = response.getOutputStream();
            // 读取服务器端返回的内容
//            System.out.println("======================响应体=========================");
            log.debug("======================响应体=========================");
            System.out.println("ResponseCode:" + conn.getResponseCode() + ",ResponseMessage:" + conn.getResponseMessage());
            if (conn.getResponseCode() == 200) {
                input = conn.getInputStream();
            } else {
                input = conn.getErrorStream();
            }

            byte[] buffer = new byte[1024 * 5];
            int len = 0;
            while ((len = input.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            out.flush();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new Exception(e);
        } finally {
            try {
                if (conn != null) {
                    conn.disconnect();
                    conn = null;
                }
                if (os != null) {
                    os.close();
                    os = null;
                }
                if (br != null) {
                    br.close();
                    br = null;
                }
            } catch (IOException ex) {
                log.error(ex.getMessage(), ex);
                throw new Exception(ex);
            }
        }
    }

    /**
     * POST Multipart Request
     *
     * @param requestUrl  请求url
     * @param requestText 请求参数
     * @param requestFile 请求上传的文件
     * @return
     * @throws Exception
     * @Description:
     */
    public static String sendRequest(String requestUrl, Map<String, String> requestText, Map<String, MultipartFile> requestFile) throws Exception {
        HttpURLConnection conn = null;
        InputStream input = null;
        OutputStream os = null;
        BufferedReader br = null;
        StringBuffer buffer = null;
        try {
            URL url = new URL(requestUrl);
            conn = (HttpURLConnection) url.openConnection();

            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setConnectTimeout(MAX_TIMEOUT);
            conn.setReadTimeout(MAX_TIMEOUT);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Accept", "*/*");
            conn.setRequestProperty("Connection", "keep-alive");
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            conn.setRequestProperty("Charset", "UTF-8");
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
            conn.connect();

            // 往服务器端写内容 也就是发起http请求需要带的参数
            os = new DataOutputStream(conn.getOutputStream());
            // 请求参数部分
            writeParams(requestText, os);
            // 请求上传文件部分
            writeFile(requestFile, os);
            // 请求结束标志
            String endTarget = PREFIX + BOUNDARY + PREFIX + LINE_END;
            os.write(endTarget.getBytes());
            os.flush();

            // 读取服务器端返回的内容
//            System.out.println("======================响应体=========================");
            log.debug("======================响应体=========================");
            System.out.println("ResponseCode:" + conn.getResponseCode() + ",ResponseMessage:" + conn.getResponseMessage());
            if (conn.getResponseCode() == 200) {
                input = conn.getInputStream();
            } else {
                input = conn.getErrorStream();
            }

            br = new BufferedReader(new InputStreamReader(input, "UTF-8"));
            buffer = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                buffer.append(line);
            }
            //......
            log.debug("返回报文:" + buffer.toString());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new Exception(e);
        } finally {
            try {
                if (conn != null) {
                    conn.disconnect();
                    conn = null;
                }
                if (os != null) {
                    os.close();
                    os = null;
                }
                if (br != null) {
                    br.close();
                    br = null;
                }
            } catch (IOException ex) {
                log.error(ex.getMessage(), ex);
                throw new Exception(ex);
            }
        }
        return buffer.toString();
    }

    /**
     * 对post参数进行编码处理并写入数据流中
     *
     * @throws Exception
     * @throws IOException
     */
    private static void writeParams(Map<String, String> requestText,
                                    OutputStream os) throws Exception {
        try {
            String msg = "请求参数部分:\n";
            if (requestText == null || requestText.isEmpty()) {
                msg += "空";
            } else {
                StringBuilder requestParams = new StringBuilder();
                Set<Entry<String, String>> set = requestText.entrySet();
                Iterator<Entry<String, String>> it = set.iterator();
                while (it.hasNext()) {
                    Entry<String, String> entry = it.next();
                    requestParams.append(PREFIX).append(BOUNDARY).append(LINE_END);
                    requestParams.append("Content-Disposition: form-data; name=\"").append(entry.getKey()).append("\"").append(LINE_END);
                    requestParams.append("Content-Type: text/plain; charset=utf-8").append(LINE_END);
                    requestParams.append("Content-Transfer-Encoding: 8bit").append(LINE_END);
                    requestParams.append(LINE_END);// 参数头设置完以后需要两个换行，然后才是参数内容
                    requestParams.append(entry.getValue());
                    requestParams.append(LINE_END);
                }
                os.write(requestParams.toString().getBytes("utf-8"));
                os.flush();
                msg += requestParams.toString();
            }
            log.debug("请求参数为：" + msg);
        } catch (Exception e) {
            log.error("writeParams failed", e);
            throw new Exception(e);
        }
    }

    /**
     * 对post上传的文件进行编码处理并写入数据流中
     *
     * @throws IOException
     */
    private static void writeFile(Map<String, MultipartFile> requestFile, OutputStream os) throws Exception {
        InputStream is = null;
        try {
            String msg = "请求上传文件部分:\n";
            if (requestFile == null || requestFile.isEmpty()) {
                msg += "空";
            } else {
                StringBuilder requestParams = new StringBuilder();
                Set<Entry<String, MultipartFile>> set = requestFile.entrySet();
                Iterator<Entry<String, MultipartFile>> it = set.iterator();
                while (it.hasNext()) {
                    Entry<String, MultipartFile> entry = it.next();
                    if (entry.getValue() == null) {//剔除value为空的键值对
                        continue;
                    }
                    requestParams.append(PREFIX).append(BOUNDARY).append(LINE_END);
                    requestParams.append("Content-Disposition: form-data; name=\"")
                            .append(entry.getKey()).append("\"; filename=\"")
                            .append(entry.getValue().getOriginalFilename()).append("\"")
                            .append(LINE_END);
                    requestParams.append("Content-Type:")
                            .append(entry.getValue().getContentType())
                            .append(LINE_END);
                    requestParams.append("Content-Transfer-Encoding: 8bit").append(
                            LINE_END);
                    requestParams.append(LINE_END);// 参数头设置完以后需要两个换行，然后才是参数内容

                    os.write(requestParams.toString().getBytes("utf-8"));
                    os.write(entry.getValue().getBytes());

                    os.write(LINE_END.getBytes());
                    os.flush();
                    msg += requestParams.toString();
                }
            }
            //System.out.println(msg);
        } catch (Exception e) {
            log.error("writeFile failed", e);
            throw new Exception(e);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (Exception e) {
                log.error("writeFile FileInputStream close failed", e);
                throw new Exception(e);
            }
        }
    }

    /**
     * ContentType(这部分可忽略，MultipartFile有对应的方法获取)
     *
     * @param file
     * @return
     * @throws IOException
     * @Description:
     */
    public static String getContentType(MultipartFile file) throws Exception {
        String streamContentType = "application/octet-stream";
        String imageContentType = "";
        ImageInputStream image = null;
        try {
            image = ImageIO.createImageInputStream(file);
            if (image == null) {
                return streamContentType;
            }
            Iterator<ImageReader> it = ImageIO.getImageReaders(image);
            if (it.hasNext()) {
                imageContentType = "image/" + it.next().getFormatName();
                return imageContentType;
            }
        } catch (IOException e) {
            log.error("method getContentType failed", e);
            throw new Exception(e);
        } finally {
            try {
                if (image != null) {
                    image.close();
                }
            } catch (IOException e) {
                log.error("ImageInputStream close failed", e);
                ;
                throw new Exception(e);
            }

        }
        return streamContentType;
    }

    //MockMultipartFile是单元测试的类，故需要单元测试的包
    //ContentType是org.apache.http.entity.ContentType
    //ContentType.APPLICATION_OCTET_STREAM.toString()可用字符串application/octet-stream代替
    public static void main(String[] args) throws Exception {
//        String requestURL = "http://192.168.140.200:8080/fastdfs/upload/file.do";
        String requestURL = "http://192.168.90.232:8095/fastdfs/upload/file.do";
        HttpIOUtil httpReuqest = new HttpIOUtil();

        Map<String, String> requestText = new HashMap<String, String>();
        requestText.put("api_key", "xxxxxxxxxxxxxxxxx");
        requestText.put("api_secret", "xxxxxxxxxxxxxxxxx");
        File file = new File("D:\\TestData\\投资者相关匹配告知书及确认函.pdf");
//        FileResponseData fileResponseData = postSendFile("http://192.168.140.200:8080/fastdfs/upload/file.do", FileUtils.readFileToByteArray(file), "许可证处理.jpg");
        FileResponseData fileResponseData = postSendFile(requestURL, FileUtils.readFileToByteArray(file), "投资者相关匹配告知书及确认函.pdf");
        log.debug(JSONObject.toJSONString(fileResponseData));

    }
}
